﻿var processPerson = function () {
    var that = this;
    this.doAction = function (state, callback) {
        if (state.action.toLowerCase() == "getpersonlist") {
            $.ajax({
                url: serverURL,
                data: state,
                success: function (d) {
                    that.renderList(eval("(" + d + ")"));
                    setTimeout(function () { that.regEvent(); }, 1000);
                }
            });
        }
        if (state.action == "getSinglePersonInfo") {
            $.ajax({
                url: serverURL,
                data: state.args,
                success: function (d) {
                    that.renderSinglePerson(eval("(" + d + ")"));
                }
            });
        }
        if (state.action == "GetPersonById") {
            this.personDb(state, callback, 1);
        }
        if (state.action == "AddPerson") {
            this.personDb(state, that.renderList, 1);
        }
        if (state.action == "EditPerson") {
            this.personDb(state, that.renderList, 1);
        }
        if (state.action == "DeletePerson") {
            this.personDb(state, that.renderList, 1);
        }
        if (state.action == "DetailPerson") {
            this.personDb(state, that.renderSinglePerson, 2);
        }
    }

    this.personDb = function (data, callback, tag) {
        $.ajax({
            url: serverURL,
            data: data,
            success: function (d) {
                if (tag == 1) {
                    callback(eval("(" + d + ")"));
                }
                if (tag == 2) {
                    callback(eval("(" + d + ")").person);
                    _processBook.showAllBooks($("#booksByPerson"), eval("(" + d + ")").books, true);
                }
            }
        });
    }

    this.renderList = function (d) {
        $("#person_list").html("");
        var html = "<table class='tab'>";
        html += "<tr><td>Id</td><td>Name</td><td>birthday</td><td>address</td><td>edit</td></tr>";
        for (var i = 0; i < d.length; i++) {
            html += "<tr>";
            html += "<td>" + d[i].id + "</td>";
            html += "<td><a tag='info' id='" + d[i].id + "'>" + d[i].name + "</a></td>";
            html += "<td>" + d[i].birthday + "</td>";
            html += "<td>" + d[i].address + "</td>";
            html += "<td><a tag='edit' id='" + d[i].id + "'>Edit</a><a tag='del' id='" + d[i].id + "'>Delete</a></td>";
            html += "</tr>";
        }
        html += "</table>";
        $("#person_list").append($(html));

    }

    this.renderSinglePerson = function (person) {
        $("#txt_personInfo_id").val(person.id);
        $("#txt_personInfo_name").val(person.name);
        $("#txt_personInfo_birthday").val(person.birthday);
        $("#txt_personInfo_address").val(person.address);
    }

    this.regEvent = function () {
        document.getElementById("person_list").addEventListener("click", function (e) {
            var tag = e.target.getAttribute("tag");
            var id = e.target.getAttribute("id");
            if (e.target.tagName.toLowerCase() == "a" && tag == "edit") {
                _controller.doAction({ action: 'EditPerson', mode: "dialog", id: id });
            }
            if (e.target.tagName.toLowerCase() == "a" && tag == "del") {
                _controller.doAction({ action: 'DeletePerson', id: id });
            }
            if (e.target.tagName.toLowerCase() == "a" && tag == "info") {
                _controller.doAction({ action: 'DetailPerson', id: id });
            }
        })
    }
}